package org.siqisource.webstart.usemanagement.helper;

import org.apache.commons.lang.StringUtils;
import org.siqisource.webstart.core.page.ActionType;
import org.siqisource.webstart.core.utils.condition.SimpleCondition;
import org.siqisource.webstart.usemanagement.model.User;
import org.siqisource.webstart.usemanagement.model.UserCtrl;

public class UserHelper {

	public static SimpleCondition getQueryCondition(UserCtrl userCtrl) {
		String actionType = userCtrl.getActionType();
		if (ActionType.ADVANCED_QUERY.equals(actionType)) {
			return getAdvancedQueryCondition(userCtrl);
		} else {
			return getKeyWordCondition(userCtrl);
		}

	}

	public static SimpleCondition getAdvancedQueryCondition(UserCtrl userCtrl) {

		SimpleCondition simpleCondition = getKeyWordCondition(userCtrl);

		User user = userCtrl.getUser();

		if (user != null) {

			// 用户名称
			String username = user.getUsername();
			if (StringUtils.isNotBlank(username)) {
				simpleCondition.andLike("username", username);
			}

		}
		return simpleCondition;
	}

	public static SimpleCondition getKeyWordCondition(UserCtrl userCtrl) {

		SimpleCondition simpleCondition = new SimpleCondition();
		
		String keyWord = userCtrl.getSearchKeyWord();
		
		
		if(StringUtils.isNotBlank(keyWord)){
			keyWord = "%"+keyWord+"%";
			
			simpleCondition.singleValue("and", "(", "username", "like", keyWord, ")");
		}

		if (StringUtils.isNotBlank(userCtrl.getSortBy()) && StringUtils.isNotBlank(userCtrl.getSortDir())) {

			simpleCondition.orderBy(userCtrl.getSortBy(), userCtrl.getSortDir());
		} else {
			simpleCondition.orderBy("username", "desc");
		}

		return simpleCondition;
	}
}
